package com.hdaccp.shuiguoD.controller;

import com.hdaccp.shuiguoD.dao.PermissionDao;
import com.hdaccp.shuiguoD.dao.RolePermissionDao;
import com.hdaccp.shuiguoD.dao.UserRoleDao;
import com.hdaccp.shuiguoD.dao.userDao;
import com.hdaccp.shuiguoD.entity.Permission;
import com.hdaccp.shuiguoD.entity.User;
import com.hdaccp.shuiguoD.entity.UserRole;
import com.hdaccp.shuiguoD.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
@Scope("prototype")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private userDao userDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Autowired
    private PermissionDao permissionDao;

    @RequestMapping("/getAll")
    @ResponseBody
    public List<User> getAll() {
        return userService.getAll();
    }

    @RequestMapping("/queryAll")
    @ResponseBody
    public Map<String, Object> queryAll(int offset, int limit) {
       /* Map<String,Object> map = new HashMap<>() ;
        map.put("rows",userInfoService.findAll(offset,limit)) ;
        map.put("total",userInfoService.findTotalCount()) ;
        return  map ;*/

        return null;
    }

    @RequestMapping("/querySearch")
    @ResponseBody
    public Map<String, Object> querySearch(String uname, int offset, int limit) {
        Map<String, Object> map = new HashMap<>();
        map.put("rows", userService.findAll(uname, offset, limit));
        map.put("total", userService.findTotalCount(uname));
        return map;

    }

    @RequestMapping("/doFenPei")
    @ResponseBody
    public Map<String, Object> doFenPei(int uid, int rid) {
        Map<String, Object> map = new HashMap<>();
        userRoleDao.update(uid, rid);
        map.put("rows", userService.findAll("", 0, 2));
        map.put("total", userService.findTotalCount(""));
        return map;
    }

    @RequestMapping("/login")
    public String login(String uname, String upwd,boolean rm, Model model, HttpServletRequest request) {

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(uname, upwd,rm);
        try {
            subject.login(token);
            upwd = this.jiami(upwd);
            User user = userDao.login(uname, upwd);
            request.getSession().setAttribute("user", user);
            UserRole userRole = userRoleDao.getRoleByUserId((int) user.getUid());
            List<Long> list = rolePermissionDao.getPermissionByRoleId((int) userRole.getRid());
            List<Permission> list1 = permissionDao.queryByPermissionIds(list);
            List<Permission> list2 = new ArrayList<>();
            for (Permission p : list1) {
                if (p.getParentid() == 0) {
                    list2.add(p);
                }
            }
            model.addAttribute("list", list2);
            model.addAttribute("list1", list1);
            //
            request.getSession().getServletContext().setAttribute("list", list2);
            request.getSession().getServletContext().setAttribute("list1", list1);
            return "redirect:/index.jsp";
        } catch (UnknownAccountException e) {
            request.getSession().setAttribute("msg", "用户名不正确!");
            return "redirect:/login.jsp";
        } catch (AuthenticationException e) {
            request.getSession().setAttribute("msg", "密码不正确!");
            return "redirect:/login.jsp";
        }
    }

    public    String jiami(String upwd){
        //所需加密的参数  即  密码
        String source = upwd ;
        //[盐] 一般为用户名 或 随机数
        String salt = "Shiro";
        //加密次数
        int hashIterations = 1024;
        SimpleHash sh = new SimpleHash("md5", source, salt, hashIterations);
        //打印最终结果
        return sh.toString() ;
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return "redirect:/login.jsp";
    }

}
